library(tidyverse)

## Geog

geog <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/geog.csv")
geog = geog %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))

geog_plot <- ggplot(geog, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Proportion In-District", "Proportion Out-of-District",
                                "Proportion (Geographically) Unknown"), values=c(15,16,17)) + 
  scale_color_manual(labels = c("Proportion In-District", "Proportion Out-of-District",
                                "Proportion (Geographically) Unknown"), values=c("black", "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(geog_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/geog_plot.pdf", width = 11, height = 6, units = "in", dpi = 300)

ideo <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/ideo.csv")
ideo = ideo %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))

ideo_plot <- ggplot(ideo, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Ideology", "In-District Ideology", "Out-of-District Ideoloy",
                                "(Geographically) Unknown Ideology"), values=c(15,16,17,18)) + 
  scale_color_manual(labels = c("Ideology", "In-District Ideology", "Out-of-District Ideoloy",
                                "(Geographically) Unknown Ideology"), values=c("black", "black", "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(ideo_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/ideo_plot.pdf", width = 11, height = 6, units = "in", dpi = 300)

sources <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/sources.csv")
sources = sources %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))


sources_plot <- ggplot(sources, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Proportion Business", "Proportion Ideological", "Proportion Individual",
                                "Proportion Labor", "Proportion Other/Unknown", "Proportion Party"), values=c(15,16,17,18,3,4)) + 
  scale_color_manual(labels = c("Proportion Business", "Proportion Ideological", "Proportion Individual",
                                "Proportion Labor", "Proportion Other/Unknown", "Proportion Party"), values=c("black", "black", "black", "black",
                                                                               "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(sources_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/sources_plot.pdf", width = 11, height = 6, units = "in", dpi = 300)


####


## Geog

geog <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/geog_postdem.csv")
geog = geog %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))

geog_plot <- ggplot(geog, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Proportion In-District", "Proportion Out-of-District",
                                "Proportion (Geographically) Unknown"), values=c(15,16,17)) + 
  scale_color_manual(labels = c("Proportion In-District", "Proportion Out-of-District",
                                "Proportion (Geographically) Unknown"), values=c("black", "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(geog_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/geog_plot_postdem.pdf", width = 11, height = 6, units = "in", dpi = 300)

ideo <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/ideo_postdem.csv")
ideo = ideo %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))

ideo_plot <- ggplot(ideo, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Ideology", "In-District Ideology", "Out-of-District Ideoloy",
                                "(Geographically) Unknown Ideology"), values=c(15,16,17,18)) + 
  scale_color_manual(labels = c("Ideology", "In-District Ideology", "Out-of-District Ideoloy",
                                "(Geographically) Unknown Ideology"), values=c("black", "black", "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(ideo_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/ideo_plot_postdem.pdf", width = 11, height = 6, units = "in", dpi = 300)

sources <- read.csv("~/Dropbox/switchers/Matching/results/APR_R&R/sources_postdem.csv")
sources = sources %>%
  mutate(match = case_when(match == 1 ~ "one",
                           match == 2 ~ "second",
                           match == 3 ~ "three"))


sources_plot <- ggplot(sources, aes(x = match, y = est, group = as.factor(outcome), color = as.factor(outcome))) +
  geom_errorbar(aes(ymin = est - 1.96*se, ymax = est + 1.96*se),
                color = "gray",
                width = 0,
                position = position_dodge(width = 0.8),
                size = .3) +
  geom_point(aes(shape = as.factor(outcome), color = as.factor(outcome), size = I(2.5)), 
             position = position_dodge(width = 0.8))+
  scale_x_discrete(labels= c("1-to-1 Match", "1-to-2 Match", "1-to-3 Match")) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.key = element_blank()) +
  theme(axis.text = element_text(size = 11, family = "Times"),
        legend.text = element_text(size = 11, family = "Times"),
        axis.title.x = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(5, 0, 0, 0), "mm")),
        axis.title.y = element_text(size = 11, family = "Times", face = "bold", 
                                    margin = unit(c(0, 5, 0, 0), "mm")),
        legend.title = element_text(size = 11, family = "Times", face = "bold")) +
  scale_shape_manual(labels = c("Proportion Business", "Proportion Ideological", "Proportion Individual",
                                "Proportion Labor", "Proportion Other/Unknown", "Proportion Party"), values=c(15,16,17,18,3,4)) + 
  scale_color_manual(labels = c("Proportion Business", "Proportion Ideological", "Proportion Individual",
                                "Proportion Labor", "Proportion Other/Unknown", "Proportion Party"), values=c("black", "black", "black", "black",
                                                                                                              "black", "black")) +
  labs(x = "Match",
       y = "ATT of Party Switching", shape = "Outcome", color = "Outcome") +
  geom_hline(yintercept = 0,  linetype="dashed",size=0.3)

ggsave(sources_plot, file = "~/Dropbox/switchers/Matching/results/APR_R&R/sources_plot_postdem.pdf", width = 11, height = 6, units = "in", dpi = 300)



## Descriptive Stats plot

outcome_by_party = read.csv("~/Dropbox/switchers/Matching/data/contribDB.csv") %>%
  distinct(cycle, bonica_rid, .keep_all = TRUE) %>%
  group_by(dem) %>%
  summarise(mean_in_district = mean(post_in_district, na.rm = TRUE),
            mean_out_district = mean(post_out_district, na.rm = TRUE),
            mean_ideo = mean(post_ideo, na.rm = TRUE),
            mean_ideo_in_district = mean(post_ideo_in_district, na.rm = TRUE),
            mean_ideo_out_district = mean(post_ideo_out_district, na.rm = TRUE),
            mean_ideo_unknown = mean(post_ideo_unknown, na.rm = TRUE),
            mean_unknown = mean(post_unknown, na.rm = TRUE),
            mean_party = mean(post_party, na.rm = TRUE),
            mean_individual = mean(post_individual, na.rm = TRUE),
            mean_business = mean(post_business, na.rm = TRUE),
            mean_labor = mean(post_labor, na.rm = TRUE),
            mean_ideological = mean(post_ideological, na.rm = TRUE),
            mean_other = mean(post_other, na.rm = TRUE)) %>%
  mutate(dem = case_when(dem == 0 ~ "Republican",
                         dem == 1 ~ "Democrat")) %>%
  rename(party = dem) %>%
  gather(var, mean, mean_in_district:mean_other, -party)


geog = outcome_by_party %>%
  filter(var %in% c("mean_in_district", "mean_out_district", "mean_unknown")) %>%
  ggplot(., aes(x = factor(var), y = mean, fill = party)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.4) +
  scale_fill_manual(values = c("blue", "red")) +
  theme_minimal() +
  theme(legend.position = "top",
        legend.title=element_blank(),
        legend.spacing.x = unit(0.2, 'cm'),
        axis.title.x = element_text(margin = unit(c(4, 0, 0, 0), "mm")),
        axis.title.y = element_text(margin = unit(c(0, 4, 0, 0), "mm"))) +
  scale_x_discrete(limits = c("mean_in_district", "mean_out_district", "mean_unknown"),
                   labels = c("In-District", "Out-District", "Unknown")) +
  labs(x = NULL,
       y = NULL)

ideo = outcome_by_party %>%
  filter(var %in% c("mean_ideo", "mean_ideo_in_district", "mean_ideo_out_district", "mean_ideo_unknown")) %>%
  ggplot(., aes(x = factor(var), y = mean, fill = party)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.4) +
  scale_fill_manual(values = c("blue", "red")) +
  theme_minimal() +
  theme(legend.position = "none",
        legend.title=element_blank(),
        legend.spacing.x = unit(0.2, 'cm'),
        axis.title.x = element_text(margin = unit(c(4, 0, 0, 0), "mm")),
        axis.title.y = element_text(margin = unit(c(0, 4, 0, 0), "mm"))) +
  scale_x_discrete(limits = c("mean_ideo", "mean_ideo_in_district", "mean_ideo_out_district", "mean_ideo_unknown"),
                   labels = c("Ideology", "In-District Ideology", "Out-District Ideology", "Unknown Ideology")) +
  labs(x = NULL,
       y = "Mean")

sources = outcome_by_party %>%
  filter(var %in% c("mean_party", "mean_business", "mean_individual", "mean_ideological", "mean_labor", "mean_other")) %>%
  ggplot(., aes(x = factor(var), y = mean, fill = party)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.4) +
  scale_fill_manual(values = c("blue", "red")) +
  theme_minimal() +
  theme(legend.position = "none",
        legend.title=element_blank(),
        legend.spacing.x = unit(0.2, 'cm'),
        axis.title.x = element_text(margin = unit(c(4, 0, 0, 0), "mm")),
        axis.title.y = element_text(margin = unit(c(0, 4, 0, 0), "mm"))) +
  scale_x_discrete(limits = c("mean_party", "mean_business", "mean_individual", "mean_ideological", "mean_labor", "mean_other"),
                   labels = c("Party", "Business", "Individual", "Ideological", "Labor", "Other")) +
  labs(x = "Outcome Variable",
       y = NULL)

descriptive = ggarrange(geog, ideo, sources,
                        ncol = 1, nrow = 3) %>%
  ggexport(filename = "~/Dropbox/switchers/Matching/results/APR_R&R/descriptive.png",
           width = 2100, height = 2600, res = 300)

